home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1136 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: lrz-muenchen.de!sun1!ua302aa
  2. From: ua302aa@sun1.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Random numbers...?
  5. Date: 11 Jan 1996 13:43:25 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4d345t$i4t@sparcserver.lrz-muenchen.de>
  9. References: <30DDBEB1.4C42@aruba.ccit.arizona.edu> <DKDz1v.736@eskimo.com> <4d2c6o$h7l@maureen.teleport.com>
  10. NNTP-Posting-Host: sun1.lrz-muenchen.de
  11.  
  12. muse@teleport.com (Andrew T Millard) writes:
  13.  
  14. >: vaughn@aruba.ccit.arizona.edu says :
  15. >: >
  16. >: >Hello,
  17. >: >this, but I need to get some code that will simply generate a random 
  18. >: >integer from 0 to 9. Specifically, I want to generate a string of 
  19. >: >four random digits. I keep generating numbers that look suspiciously 
  20. >: >non-random somehow (seeding the random number generator with the 
  21.  
  22. >Bill, this routine should do fine:
  23.  
  24. >#include <stdio.h>
  25. >#include <math.h>
  26.  
  27. There is no reason to include <math.h>, but there is a _good_ reason
  28. to include <stdlib.h>. 
  29.  
  30. >void main(void)
  31.  
  32. I will not comment on this.
  33.  
  34. >{
  35. >    int iCount, digit[4];
  36. >    srand(time());   /*  Seeds random numbers  */    
  37. >    for(iCount = 0; iCount <= 3; iCount++)
  38. >    {
  39. >        digit[iCount] = abs(rand() % 10);
  40.  
  41. Have a look at the FAQ to find out why this _could be_ a poor RNG, 
  42. depending on the implementation of rand().
  43.  
  44. >    }
  45. >}
  46.  
  47. >    If this doesn't work, then the Gods are just angry at you.  :)
  48. >                                      Newbie-man Bjornus
  49.  
  50. If this does work, it is just luck. 
  51.  
  52. Kurt
  53.  
  54. -- 
  55. | Kurt Watzka                             Phone : +49-89-2180-6254
  56. | watzka@stat.uni-muenchen.de
  57. | ua302aa@sunmail.lrz-muenchen.de
  58.